extends PanelContainer

@export var fade_seconds := 0.2

@onready var tooltip_icon: TextureRect = %TooltipIcon
@onready var tooltip_label: RichTextLabel = %TooltipLabel

var can_visible := false
var tween: Tween

func _ready() -> void:
	Events.tooltip_show_request.connect(show_tooltip)
	Events.tooltip_hide_request.connect(hide_tooltip)
	modulate = Color.TRANSPARENT
	hide()
	
func show_tooltip(icon: Texture, text: String) -> void:
	can_visible = true
	tooltip_icon.texture = icon
	tooltip_label.text = text
	if tween:
		tween.kill()
	tween = create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.tween_callback(show)
	tween.tween_property(self, "modulate", Color.WHITE, fade_seconds)
	
func hide_tooltip(force: bool) -> void:
	can_visible = false
	if tween:
		tween.kill()
	if force:
		hide()
		return
	get_tree().create_timer(fade_seconds, false).timeout.connect(hide_animation)

	
func hide_animation() -> void:
	if not can_visible:
		tween = create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CUBIC)
		tween.tween_property(self, "modulate", Color.TRANSPARENT, fade_seconds)
		tween.tween_callback(hide)
